package com.bagus.mediapipefacedetection.module.base;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bagus.mediapipefacedetection.utils.Screen;
import com.bagus.mediapipefacedetection.utils.StatusBarUtil;


public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements BaseView {
    private P mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Screen.initialize(this);
        changeAppBrightness(255);
        setContentView(getLayoutResId());
        //创建Presenter,交给子类去实现
        mPresenter = createPresenter();
        //让P层去绑定V
        boolean attachResult = mPresenter.attach(this);
        // TEST：去除attachView错误状态判断，看是否会返回false
        Log.d(getClass().getSimpleName(),"attach view: "+ attachResult);
//        if (!attachResult) {//绑定失败
//            finish();
//            return;
//        }
        StatusBarUtil.setStatusBarForGrayBg(this);
        initView();
        initData();
        mPresenter.init();
    }

    private void changeAppBrightness(int brightness) {
//        Window window = this.getWindow();
//        WindowManager.LayoutParams lp = window.getAttributes();
//        lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
//        window.setAttributes(lp);

        Window window = this.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
//        lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
        lp.screenBrightness=1;
        window.setAttributes(lp);
    }

    private void saveScreenBrightness() {
        setScrennManualMode();
        ContentResolver contentResolver = getActivity().getContentResolver();
        int value = 255; // 设置亮度值为255
        Settings.System.putInt(contentResolver,
                Settings.System.SCREEN_BRIGHTNESS, value);
    }

    public void setScrennManualMode() {
        ContentResolver contentResolver = getActivity().getContentResolver();
        try {
            int mode = Settings.System.getInt(contentResolver,
                    Settings.System.SCREEN_BRIGHTNESS_MODE);
            if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
                Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE,
                        Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
            }
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
    }

    public P getPresenter() {
        return mPresenter;
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public Context getContext() {
        return this.getApplicationContext();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    //子类Activity实现
    protected abstract void initView();

    //子类Activity实现
    protected abstract int getLayoutResId();

    //子类Activity实现
    protected abstract void initData();

    protected abstract P createPresenter();
}
